var shell = require('shelljs');
var chalk = require('chalk');

const error = chalk.bold.red;
const info = chalk.bold.cyan;
const warning = chalk.bold.yellow;

// 设置NODE_ENV被开发环境，用于区分是run dev的开发环境还是 run build的产品环境
process.env.NODE_ENV = 'development';

// 默认项目名和环境参数不需要传参数名，其他参数需要传参数名
let otherParams = {};
const neededParams = [];
params = process.argv.splice(2);
params.map(item => {
  if (item.includes('=')) {
    const temparray = item.split('=');
    otherParams[temparray[0]] = temparray[1];
  } else {
    neededParams.push(item);
  }
});
const envname = neededParams && neededParams[0];
let env = 'dev';
env = envname || env;

// const config = require('./webpack.dev.config.js');
const runCommand = `cross-env BACK_END_ENV=${env} webpack-dev-server --open --config config/webpack.dev.js`;
console.log(info('\n NOW RUNNING:\t' + runCommand + '\n'));
shell.exec(runCommand, function(code, stdout, stderr) {
  if (stderr) {
    console.log(error(stderr));
  }
});
